package com.ruoyi.generator.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.entity.ClUser;
import com.ruoyi.generator.entity.PlanItemType;
import com.ruoyi.generator.entity.PlanSchedule;
import com.ruoyi.generator.entity.bo.PlanScheduleAreaBO;
import com.ruoyi.generator.entity.bo.PlanScheduleCountBo;
import com.ruoyi.generator.entity.bo.PlanScheduleInfoBO;
import com.ruoyi.generator.entity.bo.PlanScheduleStaticInfoBO;
import com.ruoyi.generator.entity.vo.PlanScheduleVO;
import com.ruoyi.generator.entity.vo.WorkOrderVO;
import com.ruoyi.generator.mapper.WorkOrderMapper;
import com.ruoyi.generator.service.PlanScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;

/**
 * 点检计划周期状态信息 操作处理
 */
@RestController
@RequestMapping("/plan/schedule")
public class PlanScheduleController extends BaseController {
    @Autowired
    private PlanScheduleService planScheduleService;
    @Autowired
    private WorkOrderMapper workOrderMapper;

    private final ResourceLoader resourceLoader;

    public PlanScheduleController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 获取点检计划与周期状态信息列表
     *
     * @param planSchedule
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PlanSchedule planSchedule) {
        startPage();
        List<PlanScheduleInfoBO> list = planScheduleService.getPlanScheduleList(planSchedule);
        return getDataTable(list);
    }
    @GetMapping ("/listStatic")
    public TableDataInfo listStatic(PlanScheduleStaticInfoBO planSchedule) {
        startPage();
        List<PlanScheduleStaticInfoBO> list = planScheduleService.getPlanScheduleStaticList(planSchedule);
        return getDataTable(list);
    }
    //下载excel
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanScheduleStaticInfoBO planSchedule)
    {
        List<PlanScheduleStaticInfoBO> list = planScheduleService.getPlanScheduleStaticList(planSchedule);
        ExcelUtil<PlanScheduleStaticInfoBO> util = new ExcelUtil<PlanScheduleStaticInfoBO>(PlanScheduleStaticInfoBO.class);
        util.exportExcel(response, list, "设备统计");
    }

    //下载excel
    @PostMapping("/workorder/export")
    public void workorderexport(HttpServletResponse response, PlanScheduleStaticInfoBO planSchedule)
    {
        ClUser cl=new ClUser();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        cl.setUserId(user.getUserId().toString());
        cl=workOrderMapper.getUserDeptinfo(cl);
        List<WorkOrderVO> workOrders = workOrderMapper.selectAllWushuiWorkOrders(cl);
        ExcelUtil<WorkOrderVO> util = new ExcelUtil<WorkOrderVO>(WorkOrderVO.class);
        util.exportExcel(response, workOrders, "工单列表");
    }


    @GetMapping ("/listArea")
    public List<PlanScheduleAreaBO> listArea() {
        List<PlanScheduleAreaBO> list = planScheduleService.selectPlanScheduleArea();
        return list;
    }
    @GetMapping ("/listPcount")
    public List<PlanScheduleCountBo> listPcount() {
        List<PlanScheduleCountBo> list = planScheduleService.selectPlanScheduleCount();
        return list;
    }



    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody PlanScheduleVO planScheduleVO) {
        for (PlanSchedule planSchedule : planScheduleVO.getTasks()) {
            if (planSchedule.getDeviceStatus() != "" && planSchedule.getDeviceStatus() != null) {
                planScheduleService.updatePlanSchedule(planSchedule);
            } else {
                planScheduleService.updatePlanScheduleStatus(planSchedule.getPlanScheduleId());
            }
        }
        return success();
    }


    @GetMapping("/wsworkorders")
    public TableDataInfo getAllWsWorkOrders(WorkOrderVO workOrderVO) {
        startPage();
        List<WorkOrderVO> workOrders = workOrderMapper.selectAllWushuiWorkOrdersAll(workOrderVO);
        return getDataTable(workOrders);
    }

    @GetMapping("/selectEtList")
    public List<PlanItemType> selectEquipmentTypeList() {
        List<PlanItemType> workOrders = planScheduleService.selectEquipmentTypeList();
        return workOrders;
    }
    @GetMapping("/selectCiList")
    public List<PlanItemType> selectCheckItemList() {
        List<PlanItemType> workOrders = planScheduleService.selectCheckItemList();
        return workOrders;
    }

    @PostMapping("/downScFile")
    public ResponseEntity<Resource> downloadTaskFile(@RequestParam String filePath) throws IOException {

            // 假设文件存放在项目根目录的 `uploads` 文件夹
            File file = new File(filePath);
            System.out.println("uploads/" + filePath);
            // 检查文件是否存在
            if (!file.exists()) {
                return ResponseEntity.notFound().build();
            }

            // 确定文件的 MIME 类型
            String mimeType = Files.probeContentType(file.toPath());
            if (mimeType == null) {
                mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE; // 默认 MIME 类型
            }

            Resource resource = resourceLoader.getResource("file:" + file.getAbsolutePath());
            String encodedFileName = URLEncoder.encode(file.getName(), "UTF-8");
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(mimeType))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"")
                    .body(resource);

    }




}
